using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System.Xml; 
using System.Xml.Serialization; 
using System.IO; 
using System.Text; 

public class GameSaveLoad {
	
	public enum Slot{
		slot1,
		slot2,
		slot3
	}
	
	
	string FilePath{
		get{ return Application.dataPath+"\\Save";}
	}
	
	public void Save(GameData data){
		string _data = SerializeObject(data,typeof(GameData));
		FileInfo t = new FileInfo(FilePath+ "\\TopScore.xml");
		CreateXML(t,_data);
	}
	
	public bool CheckSaveFileExits(){
		if(Directory.Exists(FilePath)){
			return true;
		}
		Directory.CreateDirectory(FilePath);
		return false;
	}
	
	public GameData Load(){
		if(!CheckSaveFileExits()){
			return null;
		}
		DirectoryInfo info = new DirectoryInfo(FilePath);
		FileInfo[] fileInfo = info.GetFiles();
		foreach (FileInfo file in fileInfo) {
			if(file.Name.Contains("TopScore")){
				string _data = LoadXML(file);
				if(_data.ToString() != "") 
      			{ 
					return (GameData)DeserializeObject(_data,typeof(GameData));
      			}
			}
			 
		}
		return null;
	}
	
	string UTF8ByteArrayToString(byte[] characters) 
   {      
      UTF8Encoding encoding = new UTF8Encoding(); 
      string constructedString = encoding.GetString(characters); 
      return (constructedString); 
   } 
 
   byte[] StringToUTF8ByteArray(string pXmlString) 
   { 
      UTF8Encoding encoding = new UTF8Encoding(); 
      byte[] byteArray = encoding.GetBytes(pXmlString); 
      return byteArray; 
   } 
 
   // Here we serialize our UserData object of myData 
   string SerializeObject(object pObject,System.Type type) 
   { 
      MemoryStream memoryStream = new MemoryStream(); 
      XmlSerializer xs = new XmlSerializer(type);
      XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); 
      xs.Serialize(xmlTextWriter, pObject); 
      memoryStream = (MemoryStream)xmlTextWriter.BaseStream; 
      return UTF8ByteArrayToString(memoryStream.ToArray());  
   } 
 
   // Here we deserialize it back into its original form 
   object DeserializeObject(string pXmlizedString,System.Type type) 
   { 
      XmlSerializer xs = new XmlSerializer(type); 
      MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString)); 
      //XmlTextReader reader = new XmlTextReader(memoryStream); 
      return xs.Deserialize(memoryStream); 
   } 
 
   // Finally our save and load methods for the file itself 
   void CreateXML(FileInfo t, string _data) 
   { 
		StreamWriter writer;
      if(!t.Exists) 
      { 
         writer = t.CreateText(); 
      } 
      else 
      { 
         t.Delete(); 
         writer = t.CreateText(); 
      } 
      writer.Write(_data); 
      writer.Close(); 
      Debug.Log("File written."); 
   } 
 
   string LoadXML(FileInfo file) 
   { 
      StreamReader r = file.OpenText(); 
      string _info = r.ReadToEnd(); 
      r.Close(); 
      return _info;
   } 
}

public class GameData{
	public List<int> TopScores;
	public GameData(){
		TopScores = new List<int>();
	}
}